<?php

declare (strict_types = 1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace HyperfTest;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\CoroutineHandler;
use Hyperf\Testing\Client;
use PHPUnit\Framework\TestCase;

/**
 * Class HttpTestCase.
 * @method get($uri, $data = [], $headers = [])
 * @method post($uri, $data = [], $headers = [])
 * @method json($uri, $data = [], $headers = [])
 * @method file($uri, $data = [], $headers = [])
 */
abstract class HttpTestCase extends TestCase {
    /**
     * @var Client
     */
    protected $client;

    public function __construct($name = null, array $data = [], $dataName = '') {
        parent::__construct($name, $data, $dataName);
        $this->client = make(Client::class);
    }

    public function __call($name, $arguments) {
        return $this->client->{$name}(...$arguments);
    }
    protected function getToken($account, $password) {
        $client = new GuzzleClient([
            'base_uri' => 'http://127.0.0.1:9501',
            'handler' => HandlerStack::create(new CoroutineHandler()),
            'timeout' => 5,
            'swoole' => [
                'timeout' => 10,
                'socket_buffer_size' => 1024 * 1024 * 2,
            ],
        ]);
        $response = $client->post('/login', [
            'form_params' => [
                'account' => 'account',
                'password' => '1',
            ],
        ]);
        //$this->assertTrue(200 == $response->getStatusCode());
        $responseBody = $response->getBody();
        $rt = json_decode($responseBody->read($responseBody->getSize()), true);
        if (isset($rt['data']['token'])) {
            $token = $rt['data']['token'];
        } else {
            $token = '';
        }
        return $token;
    }
}
